﻿@model IList<Order>
@inject Grand.Services.Catalog.IProductService _productService;
@inject Grand.Services.Directory.ICountryService _countryService;
@inject Grand.Services.Configuration.ISettingService _settingService;
@inject Grand.Services.Stores.IStoreService _storeService;
@inject Grand.Core.IWorkContext _workContext;
@inject IStoreContext _storeContext;
@inject Grand.Services.Media.IPictureService _pictureService;
@inject PdfSettings _pdfSettings;
@inject Grand.Services.Localization.ILocalizationService _localizationService;
@inject Grand.Services.Payments.IPaymentService _paymentService;
@inject Grand.Services.Directory.ICurrencyService _currencyService;
@inject Grand.Services.Orders.IGiftCardService _giftCardService;
@using Grand.Services.Localization;

@{
    var logoPicture = await _pictureService.GetPictureById(_pdfSettings.LogoPictureId);
    var logoExists = logoPicture != null;
    var companyName = _storeContext.CurrentStore.CompanyName;
    var limitbyVendor = _workContext.CurrentVendor != null;
}
<style>
    html {
        font-size: 18px;
    }

    .column {
        display: flex;
        flex-direction: column;
        flex-basis: 100%;
        flex: 1;
    }

    .col-container {
        display: table;
        width: 100%;
        margin-top: 15px;
    }

        .col-container .column {
            display: table-cell;
        }

        .col-container .right.column img {
            margin-top: 25px;
        }

    ul {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .addresses .billing {
        width: 50%;
        float: left;
    }

    .addresses .shipping {
        width: 50%;
        float: right;
    }

    .orderItems {
        width: 100%;
        border: 1px solid black;
    }

        .orderItems tr th {
            background-color: #f5f5f5;
            text-align: left;
            padding: 0 5px 0 5px;
        }

        .orderItems tr,
        .orderItems tr td,
        .orderItems tr th {
            border: 1px solid black;
            padding: 0 5px 0 5px;
        }

    .summary {
        margin-top: 20px;
    }

    .right {
        float: right;
    }

    .footer {
        position: absolute;
        bottom: 0;
        width: 100%;
    }
</style>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    @{ var i = 0; }
    @foreach (var order in Model)
    {
        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceHeaderText))
        {
            <div>@_pdfSettings.InvoiceHeaderText</div>
        }
        <div class="col-container">
            <div class="left column"><h2>@companyName</h2></div>
            <div class="right column"><img src="@(logoExists ? await _pictureService.GetPictureUrl(logoPicture, 0, false, _storeContext.CurrentStore.Url) : "" )" /></div>
        </div>
        <h2>@(string.Format(_localizationService.GetResource("PDFInvoice.Order#", order.CustomerLanguageId), order.OrderNumber))</h2>
        <div class="addresses">
            @if (order.BillingAddress != null)
            {
                <div class="billing column">
                    <h3>@(_localizationService.GetResource("PDFInvoice.BillingInformation", order.CustomerLanguageId))</h3>
                    <ul>
                        @if (!string.IsNullOrEmpty(order.BillingAddress.Company))
                        {
                            <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.BillingAddress.Company))</li>
                        }
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.BillingAddress.FirstName + " " + order.BillingAddress.LastName))</li>
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.BillingAddress.PhoneNumber))</li>
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.BillingAddress.Address1))</li>
                        <li>@(order.BillingAddress.City + ", " + order.BillingAddress.ZipPostalCode)</li>
                        <li>@((await _countryService.GetCountryById(order.BillingAddress.CountryId))?.Name)</li>
                        @{
                            var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
                            string paymentMethodStr = paymentMethod != null ? paymentMethod.GetLocalizedFriendlyName(_localizationService, order.CustomerLanguageId) : order.PaymentMethodSystemName;
                        }
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.PaymentMethod", order.CustomerLanguageId), paymentMethodStr))</li>
                    </ul>
                </div>
            }
            @if (order.ShippingAddress != null)
            {
                <div class="shipping column">
                    <h3>@(_localizationService.GetResource("PDFInvoice.ShippingInformation", order.CustomerLanguageId))</h3>
                    <ul>
                        @if (!string.IsNullOrEmpty(order.ShippingAddress.Company))
                        {
                            <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.ShippingAddress.Company))</li>
                        }
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName))</li>
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.ShippingAddress.PhoneNumber))</li>
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.ShippingAddress.Address1))</li>
                        <li>@(order.ShippingAddress.City + ", " + order.ShippingAddress.ZipPostalCode)</li>
                        <li>@((await _countryService.GetCountryById(order.ShippingAddress.CountryId))?.Name)</li>
                        <li>@(string.Format(_localizationService.GetResource("PDFInvoice.ShippingMethod", order.CustomerLanguageId), order.ShippingMethod))</li>
                    </ul>
                </div>
            }
        </div>

        <div style="display:block; clear:both;"></div>
        <div class="summary">
            <h3>@(_localizationService.GetResource("PDFInvoice.Product(s)", order.CustomerLanguageId))</h3>
        </div>
        <table class="orderItems">
            <tr>
                <th>@(_localizationService.GetResource("PDFInvoice.ProductName", order.CustomerLanguageId))</th>
                <th>@(_localizationService.GetResource("PDFInvoice.ProductPrice", order.CustomerLanguageId))</th>
                <th>@(_localizationService.GetResource("PDFInvoice.ProductQuantity", order.CustomerLanguageId))</th>
                <th>@(_localizationService.GetResource("PDFInvoice.ProductTotal", order.CustomerLanguageId))</th>
            </tr>
            @foreach (var item in order.OrderItems.Where(x=> x.VendorId == _workContext.CurrentVendor?.Id || !limitbyVendor).ToList())
            {
                <tr>
                    <td>
                        @((await _productService.GetProductByIdIncludeArch(item.ProductId)).Name)
                        @if (!string.IsNullOrEmpty(item.AttributeDescription))
                        {
                            <br />
                            <small>@(Html.Raw(item.AttributeDescription))</small>
                        }
                    </td>
                    <td>@item.UnitPriceInclTax</td>
                    <td>@item.Quantity</td>
                    <td>@item.PriceInclTax</td>
                </tr>
            }
        </table>
        @if (!limitbyVendor)
        {
            <div class="summary">
                <hr />
                <div class="right">
                    <ul>
                        <li>@(_localizationService.GetResource("PDFInvoice.Sub-Total", order.CustomerLanguageId)) @order.OrderSubtotalInclTax @order.CustomerCurrencyCode</li>
                        @if (order.OrderSubTotalDiscountInclTax > decimal.Zero)
                        {
                            <li>@(_localizationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId)) @order.OrderSubTotalDiscountInclTax @order.CustomerCurrencyCode</li>
                        }
                        @if (order.PaymentMethodAdditionalFeeInclTax > decimal.Zero)
                        {
                            <li>@(_localizationService.GetResource("PDFInvoice.PaymentmethodAdditionalFee", order.CustomerLanguageId)) @order.PaymentMethodAdditionalFeeInclTax @order.CustomerCurrencyCode</li>
                        }
                        <li>@(_localizationService.GetResource("PDFInvoice.Shipping", order.CustomerLanguageId)) @order.OrderShippingInclTax @order.CustomerCurrencyCode</li>
                        @if (order.OrderDiscount > decimal.Zero)
                        {
                            <li>@(_localizationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId)) @order.OrderDiscount @order.CustomerCurrencyCode</li>
                        }
                        @foreach (var gcuh in await _giftCardService.GetAllGiftCardUsageHistory(order.Id))
                        {
                            if (gcuh.UsedValue > decimal.Zero)
                            {
                                var giftcard = await _giftCardService.GetGiftCardById(gcuh.GiftCardId);
                                <li>@(string.Format(_localizationService.GetResource("PDFInvoice.GiftCardInfo", order.CustomerLanguageId), giftcard.GiftCardCouponCode)) @gcuh.UsedValue</li>
                            }
                        }
                        @if (order.OrderTax > decimal.Zero)
                        {
                            <li>@(_localizationService.GetResource("PDFInvoice.Tax", order.CustomerLanguageId)) @order.OrderTax @order.CustomerCurrencyCode</li>
                        }
                        <li>@(_localizationService.GetResource("PDFInvoice.OrderTotal", order.CustomerLanguageId)) @order.OrderTotal @order.CustomerCurrencyCode</li>
                    </ul>
                </div>
            </div>
        }
        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceFooterText))
        {
            <footer class="footer">
                @_pdfSettings.InvoiceFooterText
            </footer>
        }
        if (Model.Count - 1 > i)
        {
            <div style="display:block; clear:both; page-break-after:always;"></div>
        }
        i++;
    }

</body>
</html>